package com.woniu.controller;

import com.woniu.entity.Category;
import com.woniu.service.CategoryService;
import com.woniu.service.ProductService;
import com.woniu.util.ResponseUtil;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;


import javax.annotation.Resource;

@RestController
@RequestMapping("/category")
public class CategoryController {
@Resource
private ProductService productService;
@Resource
private CategoryService categoryService;

 // 根据categoryId 查询食物分类
    @GetMapping("/{categoryId}")
    public ResponseUtil findById(@PathVariable Integer categoryId) {
        return categoryService.findByCategoryId(categoryId);
    }
    //查询所有的食品分类
    @GetMapping()
    public ResponseUtil findAllCategory() {
        return categoryService.findAll();

    }
    // 根据categoryId 查询分类下的所有商品
    @GetMapping("/findProduct/{categoryId}")
    public ResponseUtil findProduct(@PathVariable Integer categoryId) {
        return productService.categoryFindAll(categoryId);}


    @GetMapping("/findAll2")
    public ResponseUtil findAll2(){
        return categoryService.findAll2();
    }
    @PutMapping("/update")
    public ResponseUtil update(@RequestBody Category category){
        return categoryService.update(category);
    }


    @GetMapping("/findById/{categoryId}")
    public ResponseUtil findById(@PathVariable String categoryId){
        return categoryService.findById(Integer.valueOf(categoryId));
    }

        @PutMapping("/updateStatus")
        public ResponseUtil updateStatus (@RequestBody Category category){
            return categoryService.updateStatus(category);
        }
        @DeleteMapping("/delete/{categoryId}")
        public ResponseUtil del (@PathVariable String categoryId){
            return categoryService.delete(Integer.valueOf(categoryId));
        }
        @PostMapping("/add")
        public ResponseUtil add (@RequestBody Category category){
            return categoryService.add(category);
        }
        @PostMapping("/addSubCategory/{name}/{typeId}")
        public ResponseUtil addSubCategory (@PathVariable String name
                , @PathVariable String typeId ){
            return categoryService.addSubCategory(name, Integer.valueOf(typeId));
        }
        @GetMapping("/findByName/{name}")
        public ResponseUtil findByName (@PathVariable String name){
            return categoryService.findByName(name);
        }


    }

